import { get } from "../../api/user"


export default ({
    namespaced: true,
    state: {
        token: localStorage.getItem('token') || '',
        userinfo: localStorage.getItem('userinfo') || ''
    },
    mutations: {
        // 保存token
        updaToken(state, payload) {

            state.token = payload
            // 持久化
            localStorage.setItem('token', payload)
        },
        // 保存个人信息
        updaUserinfo(state, payload) {

            state.userinfo = payload
            // 持久化
            localStorage.setItem('userinfo', payload)
        },
        // 删除token
        delToken(state) {
            state.token = ''
            // 清空localStorage中的token
            localStorage.removeItem('token')
        },
        // 删除个人信息
        delUserinfo(state) {
            state.userinfo = {}
            // 持清空内存中的 userinfo
            localStorage.removeItem('userinfo')

        }
    },
    actions: {
        async getUserinfo(context) {
            console.log('context', context);
            const res = await get()
            console.log('获取用户资料', res); //res.data
            context.commit('updaUserinfo', res.data)
        },
    },

})
